package org.ycsoft.metroid;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.ycsoft.MetroidReader.LineRecord;
import org.ycsoft.MetroidReader.MetroidMetadata;
import org.ycsoft.MetroidReader.MetroidReader;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class LinesList extends Activity {

	private String mapFile;
	private ListView showLines;
	private TextView atTop;
	private MetroidReader reader;
	private Handler handler;
	private List<Integer> linesIds;
	private List<String> linesNames;
	private Set<String> linesRealNames;
	private ProgressDialog dlgProgress;
	public String city;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.lines);
		atTop = (TextView) findViewById(R.id.txttop);

		showLines = (ListView) findViewById(R.id.listLines);
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			// Wrong place!! how u get here!!
			finish();
		} else {
			mapFile = extras.getString(Commons.KEY_MAPFILE);
			city = extras.getString(Commons.KEY_MAPNAME);
			atTop.setText(city + Commons.KEY_SEP
					+ getString(R.string.lines_list));

		}

		handler = new Handler();
		Thread linesListLoader = new Thread(new Runnable() {
			public void run() {
				// load map data
				try {
					loadLinesList();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		});
		linesListLoader.start();
	}

	public void loadLinesList() throws IOException {
		synchronized (handler) {
			handler.post(new Runnable() {
				public void run() {
					dlgProgress = ProgressDialog.show(LinesList.this,
							Commons.TAG, getResources().getString(
									R.string.load_lines_list), true, false);
				}

			});

		}

		reader = new MetroidReader(mapFile);
		reader.loadIndexes(true);
		linesIds = new ArrayList<Integer>(reader.getLinesIdList());
		linesNames = new ArrayList<String>();
		for (int line : linesIds) {
			linesNames.add(reader.getLineFromId(line).toString());
			// linesRealNames.add(reader.getLineFromId(line).getLineName());
		}
		reader.closeStream();
		deleteDuplicates();
		final ArrayAdapter<String> linesListAdapter = new ArrayAdapter<String>(
				this, R.layout.listbold, linesNames);
		synchronized (handler) {
			handler.post(new Runnable() {
				public void run() {
					showLines.setAdapter(linesListAdapter);
					showLines.setOnItemClickListener(goToLineDetails);
				}
			});
		}
		dlgProgress.dismiss();
	}

	public void deleteDuplicates() {
		linesRealNames = new HashSet<String>();
		String realName, tmp;
		Iterator<String> dead = linesNames.iterator();
		while (dead.hasNext()) {
			tmp = dead.next();
			realName = tmp.split(":")[0];
			if (!linesRealNames.add(realName)) {
				linesIds.remove(linesNames.indexOf(tmp));
				dead.remove();
			}
		}
	}

	private OnItemClickListener goToLineDetails = new OnItemClickListener() {

		public void onItemClick(AdapterView arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			showLine(position);
		}

	};

	public void showLine(int pos) {
		Intent i = new Intent(this, LineDetails.class);
		i.putExtra(Commons.KEY_LINE_ID, String.valueOf(linesIds.get(pos)));
		i.putExtra(Commons.KEY_LINE_NAME, linesNames.get(pos).split(":")[0]);
		i.putExtra(Commons.KEY_CITY, city);
		i.putExtra(Commons.KEY_MAP, mapFile);
		startActivity(i);
		// finish();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		try {
			if (reader != null && !reader.isStreamClosed()) {
				reader.closeStream();
			}
		} catch (Exception e) {
			Log.e(Commons.TAG, e.getMessage());
		}
	}

	public void onpause() {
		super.onPause();
		try {
			if (reader != null && !reader.isStreamClosed()) {
				reader.closeStream();
			}
		} catch (Exception e) {
			Log.e(Commons.TAG, e.getMessage());
		}
	}

}
